/*
 * sdlwindow.cpp
 *
 *  Created on: 30.06.2011
 *      Author: marcell
 */



#include <sdlwindow.h>
#include <SDL_syswm.h>

ceSDLWindow::ceSDLWindow ()
: surface (0)
{
  CE_OBJECT_CONSTR;
}

ceSDLWindow::~ceSDLWindow ()
{
  if (surface)
    {
      SDL_FreeSurface(surface);
      surface = 0;
    }
}


bool ceSDLWindow::createWindow ( unsigned width,
                               unsigned height,
                               unsigned depth,
                               bool     fullscreen,
                               unsigned rColor,
                               unsigned gColor,
                               unsigned bColor,
                               unsigned aColor,
                               unsigned doubleBuffer,
                               unsigned depthSize,
                               unsigned stencilSize)
{
  SDL_GL_SetAttribute(SDL_GL_RED_SIZE,      rColor);
  SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,    gColor);
  SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,     bColor);
  SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE,    aColor);
  SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,  doubleBuffer);
  SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,    depthSize);
  SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE,  stencilSize);
  SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, rColor);
  SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, gColor);
  SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, bColor);
  SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, aColor);
  SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);
  SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES,  0);
  SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL,  0);

  unsigned flags = SDL_OPENGL;
  if (fullscreen)
    {
      flags |= SDL_FULLSCREEN;
    }
  surface = SDL_SetVideoMode(width, height, depth, flags);

  SDL_SysWMinfo info;
  /*
  SDL_VERSION(&info.version); // this is important!
  if (SDL_GetWMInfo(&info) > 0)
    {
#ifdef WIN32
//        windowHandle = info.window;
//        SetWindowPos (info.window, NULL, 400, 20, width, height, 0);
#else
        windowHandle = info.info.x11.wmwindow;
#endif
    }
  */

  return surface != 0;
}

int ceSDLWindow::GetX() const
{
  return 0;
}

int ceSDLWindow::GetY() const
{
  return 0;
}

unsigned ceSDLWindow::GetWidth() const
{
  return surface->w;
}

unsigned ceSDLWindow::GetHeight() const
{
  return surface->h;
}

bool ceSDLWindow::IsFullscreen() const
{
  return (surface->flags & SDL_FULLSCREEN) == SDL_FULLSCREEN;
}

ceWindowHandle ceSDLWindow::GetWindowHandle ()
{
    return windowHandle;
}

